"""
@Time ： 2023/10/4 0004 9:03
@Auth ： zpt
@File ：image_build_page.py
@IDE ：PyCharm
"""

import time

from selenium.webdriver.common import keys

from Page.base_page import BasePage
from Page.login_page import LoginPage
from Lib.readelement import Element
from Lib.log import log

all_project = Element('all_project')


class AllProjects(BasePage):
    """
     所有工程页面
     """
    cfg_name = "everything"
    cfg_repo = "https://repo.openeuler.org/openEuler-22.03-LTS/everything/"
    name = "python-flask"
    address = "https://gitee.com/src-openeuler/python-flask.git"
    url_name = "custom"
    url_address = "https://gitee.com/raomingbo/customization-base.git"
    url_new_address = "https:///gitee.com/zpt/customization-base.git"
    url_bad_address = "https://gitee.com/zpt/customization-base.git"

    def login(self, url=None, name=None, password=None):
        """
        用户登录
        """
        login = LoginPage(self.driver)
        login.login(url, name, password)

    def alert_message(self):
        """
        获取弹窗元素
        """
        self.mouse_move(all_project["assert"])
        alert = self.find_element(all_project["assert"])

        return alert.text

    def check_elements(self, eles_list):
        """
         页面信息查询
        """
        try:
            flag = True
            for ele in eles_list:
                log.info('check element ' + ele)
                flag = self.is_element_exist(all_project[ele])
                if not flag:
                    break
            return flag
        except Exception as e:
            log.error('check element ' + ele + ' failed' + 'flag:' + str(flag), e)

    def public_projects(self):
        """
        公有工程按钮
        """
        try:
            self.click(all_project['public_projects_btn'])
        except Exception as e:
            log.info("error: found public projects btn timeout")
            log.info(e)


    def private_projects(self):
        """
        私有工程按钮
        """
        self.click(all_project['private_projects_btn'])

    def project_list(self):
        """
        工程列表信息检查
        """
        name_text = self.get_element_text(all_project['name'])
        description_text = self.get_element_text(all_project['description'])
        create_time_text = self.get_element_text(all_project['create_time'])

        return [name_text, description_text, create_time_text]

    def add_project_btn(self):
        """
        添加工程按钮
        """
        self.click(all_project['add_project_btn'])

    def add_project_view_cancel(self):
        """
        添加工程取消按钮
        """
        try:
            self.click(all_project['add_project_cancel'])
        except Exception as e:
            log.info("error: found public add project cancel timeout")
            log.info(e)

    def add_project_view_close(self):
        """
        添加工程取消按钮
        """
        try:
            self.click(all_project['add_project_close'])
        except Exception as e:
            log.info("error: found public add project close timeout")
            log.info(e)

    def add_project_name(self, name):
        """
        添加工程名称
        """
        self.send_keys(all_project['add_project_input_box'], name)

    def add_project_confirm_btn(self):
        """
        添加工程确认按钮
        """
        try:
            self.click(all_project['project_confirm_btn'])
        except Exception as e:
            log.info("error: found public project confirm btn timeout")
            log.info(e)

    def get_first_project(self):
        """
       获取第一个工程的工程名
       """
        name = self.get_element_text(all_project['first_project'])
        return name

    def enter_first_project(self):
        """
        添加工程确认按钮
        """
        self.click(all_project['first_project'])

    def set_project_search_box(self, name):
        """
        project搜索框输入搜索内容
        """
        self.send_keys(all_project['search_project'], name)
        time.sleep(2)
        self.enter_key(all_project['search_project'])

    def get_empty_text(self):
        """
        获取暂无数据文本
        """
        text = self.get_element_text(all_project['empty_text'])
        return text

    def project_veiw_name(self):
        """
        project view界面工程名
        """
        name = self.get_element_text(all_project['project_view_name'])
        return name

    def add_project(self, name):
        """
        添加工程
        """
        self.add_project_btn()
        time.sleep(2)
        self.send_keys(all_project['add_project_input_box'], name)
        self.add_project_confirm_btn()

    def add_package_btn_close(self):
        self.click(all_project['add_package'])
        time.sleep(2)
        self.click(all_project['add_package_close'])

    def add_package_btn_cancel(self):
        self.click(all_project['add_package'])
        time.sleep(2)
        self.click(all_project['add_package_cancel'])

    def edit_description(self, name):
        self.click(all_project['description_btn'])
        self.send_keys(all_project['description_input'], name)
        self.click(all_project['description_btn'])

    def add_package_confirm(self):
        self.click(all_project['add_package'])
        time.sleep(2)
        self.send_keys(all_project['pacakge_input'], self.name)
        self.send_keys(all_project['git_address'], self.address)
        self.click(all_project['add_package_confirm'])

    def enter_config_view(self):
        self.click(all_project['config'])
        time.sleep(1)

    def enter_user_view(self):
        self.click(all_project['user_page'])
        time.sleep(1)

    def add_rpm_repo_add_target(self):
        self.click(all_project['config'])
        time.sleep(1)
        self.click(all_project['add_build_target'])
        time.sleep(2)
        self.click(all_project['os_variant'])
        time.sleep(2)
        self.click(all_project['choose_variant'])
        time.sleep(2)
        self.click(all_project['arch'])
        time.sleep(2)
        self.click(all_project['choose_arch'])
        time.sleep(2)
        self.click(all_project['os_variant_confirm'])
        self.click(all_project['add_repo'])
        time.sleep(2)
        self.send_keys(all_project['repo_name'], self.cfg_name)
        time.sleep(2)
        self.send_keys(all_project['repo_address'], self.cfg_repo)
        time.sleep(2)
        self.click(all_project['repo_confirm'])

    def project_overview(self):
        self.click(all_project['project_overview'])

    def goto_first_package(self):
        self.click(all_project['first_package'])

    def goto_build_history(self):
        self.click(all_project['build_history'])

    def click_full_build(self):
        self.project_overview()
        time.sleep(2)
        self.click(all_project['full_build'])
        message = self.alert_message()
        return message

    def click_incremental_build(self):
        self.project_overview()
        time.sleep(2)
        self.click(all_project['incremental_build'])
        message = self.alert_message()
        return message

    def single_build(self):
        self.project_overview()
        time.sleep(2)
        self.goto_first_package()
        time.sleep(3)
        self.click(all_project['single_build'])
        message = self.alert_message()
        return message

    def specify_incremental_build(self):
        self.project_overview()
        time.sleep(2)
        self.goto_first_package()
        time.sleep(3)
        self.click(all_project['specify_incremental_build'])
        message = self.alert_message()
        return message

    def delete_my_project(self):
        self.enter_first_project()
        time.sleep(2)
        text = self.get_element_text(all_project['delete'], 'aria-disabled')
        print(text)
        # if text == "ture":
        #     print("不是本次测试创建的工程")
        # else:
        #     delete_btn.click()
        #     time.sleep(5)
        #     info = self.driver.find_element(By.XPATH, '//div/div[3]/div[3]/div[@aria-label="删除"]/div/div/div').text
        #     # 删除确认按钮
        #     self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div/div[3]/div['
        #                                        '3]/div/div/footer/div/span/button[1]/span').click()
        #     time.sleep(5)
        #
        #     return info

    def click_add_urls(self):
        self.enter_first_project()
        time.sleep(2)
        self.enter_config_view()
        time.sleep(2)
        self.click(all_project['add_layer_btn'])
        time.sleep(2)

    def click_add_urls_cancel(self):
        self.click(all_project['url_cancel'])

    def click_add_urls_close(self):
        self.click(all_project['url_close'])

    def click_add_urls_confirm(self):
        self.click(all_project['url_confirm'])

    def config_url_input(self):
        self.send_keys(all_project['url_name_input'], self.url_name)
        time.sleep(1)
        self.send_keys(all_project['url_input'], self.url_address)
        time.sleep(1)
        self.click_add_urls_confirm()

    def get_url_text(self):
        url_name_text = self.get_element_text(all_project['my_url_name'])
        url_address_text = self.get_element_text(all_project['my_url_address'])

        return [url_name_text, url_address_text]

    def edit_url(self):
        self.click(all_project['url_edit'])
        time.sleep(1)
        self.clear_and_send_keys(all_project['edit_url_input'], self.url_new_address)
        time.sleep(1)
        self.click(all_project['url_save'])
        save_data = self.get_url_text()
        return save_data

    def edit_bad_format_url(self):
        self.click(all_project['url_edit'])
        time.sleep(1)
        self.clear_and_send_keys(all_project['edit_url_input'], self.url_bad_address)
        time.sleep(1)
        self.click(all_project['url_save'])
        message = self.alert_message()
        return message

    def delete_confirm_url(self):
        self.click(all_project['url_delete'])
        time.sleep(1)
        self.click(all_project['url_delete_confirm'])
        time.sleep(1)

    def delete_cancel_url(self):
        self.click(all_project['url_delete'])
        time.sleep(1)
        self.click(all_project['url_delete_cancel'])
        time.sleep(1)

    def delete_close_url(self):
        self.click(all_project['url_delete'])
        time.sleep(1)
        self.click(all_project['url_delete_close'])
        time.sleep(1)

    def config_layer_url_project(self):
        self.click_add_urls()
        time.sleep(2)
        self.config_url_input()
        time.sleep(2)
        self.click(all_project['add_build_target'])
        time.sleep(2)
        self.click(all_project['os_variant'])
        time.sleep(2)
        self.click(all_project['choose_variant'])
        time.sleep(2)
        self.click(all_project['arch'])
        time.sleep(2)
        self.click(all_project['choose_arch'])
        time.sleep(2)
        self.click(all_project['os_variant_confirm'])
        self.click(all_project['add_repo'])
        time.sleep(2)
        self.send_keys(all_project['repo_name'], self.cfg_name)
        time.sleep(2)
        self.send_keys(all_project['repo_address'], self.cfg_repo)
        time.sleep(2)
        self.click(all_project['repo_confirm'])
